package controllers

import (
	"github.com/astaxie/beego"
	"travelservia/models"
)

type ZixunController struct {
	beego.Controller
}
func (c *ZixunController) Prepare()  {
	c.Data["headlink"]="zixun"
}

func (c *ZixunController)GetList() {
	typestr := c.GetString("type")
	tacleZixunSearch(&c.Controller)
	zixuns := []models.Zixun{}
	counts, err := models.GetZixunListCount()
	if err != nil {
		Message(&c.Controller, `/`, "获取列表错误即将返回首页", "", false)
	}
	numPerPage := 40
	p := SetPaginator(&c.Controller, numPerPage, counts)
	CurrentPage := p.Page()

	if typestr == "hot" {
		models.GetZixunHotList(&zixuns, (CurrentPage - 1) * numPerPage, numPerPage)
	}else if typestr=="newest"{
		models.GetZixunList(&zixuns, (CurrentPage - 1) * numPerPage, numPerPage)
	}else{
		models.GetZixunList(&zixuns, (CurrentPage - 1) * numPerPage, numPerPage)
	}
	c.Data["zixun"]=&zixuns
	c.Data["listpage"]=true
	c.TplName="zixunlist.html"
}
func tacleZixunSearch(c *beego.Controller) {
	search:=c.GetString("search")
	if search==""{
		return
	}
	zixuns := []models.Zixun{}
	counts, err := models.GetZixunSearchCount(search)
	if err != nil {
		Message(c, `/`, "获取列表错误即将返回首页", "", false)
	}
	numPerPage := 40
	p := SetPaginator(c, numPerPage, counts)
	CurrentPage := p.Page()
	models.GetZixunHotList(&zixuns, (CurrentPage - 1) * numPerPage, numPerPage)
	c.Data["zixun"]=&zixuns
	c.TplName="zixunlist.html"
	c.Render()
	c.StopRun()
}


func (c *ZixunController)GetDetail()  {
	id,_:=c.GetInt("id")
	zixun:=models.Zixun{Id:id}
	if err:=models.GetZixunDetail(&zixun);err!=nil{
		Message(&c.Controller, `/`, "获取列表错误即将返回首页", "", false)
	}
	c.Data["zixun"]=&zixun
	c.TplName="zixundetail.html"
}

func (c *ZixunController)AddBrowseCount() {
	id,_:=c.GetInt("id")
	if id==0{
		Json_Message(&c.Controller,1,"id not right")
	}
	zixun:=models.Zixun{Id:id}
	if err:=models.AddZixunBrowseCount(&zixun);err!=nil{
		Json_Message(&c.Controller,1,"添加计数失败")
	}
	Json_Message(&c.Controller,0,"success！")
}
func (c *ZixunController)AddStartCount() {
	id,_:=c.GetInt("id")
	if id==0{
		Json_Message(&c.Controller,1,"id not right")
	}
	zixun:=models.Zixun{Id:id}
	if err:=models.AddZixunStarCount(&zixun);err!=nil{
		Json_Message(&c.Controller,1,"添加点赞失败")
	}
	Json_Message(&c.Controller,0,"success！")
}